/**
 * 
 */
package com.example.android_is2;

//import com.example.android_is2.Route.MapRouteActivity;

import com.example.android_is2.trackerGeoPoint.TrackerActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;


/**
 * @author KAIN-PC
 * 
 */
public class MenuActivity extends Activity {

  private static final int Error_Net = 0;

  public static boolean statusError = false;

  private String Log_app = null;

  private LocationManager locationManager = null;

  private LocationProvider netProvider = null;

  private BridgeLocation bridge = null;

  private final String defaultLocationManager = LocationManager.GPS_PROVIDER;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    netProvider = locationManager.getProvider(defaultLocationManager);

    // Sezione Bottoni
    Button btn_menu_001 = (Button) findViewById(R.id.menu_button1);
    btn_menu_001.setOnClickListener(new View.OnClickListener() {

      @SuppressWarnings("deprecation")
      public void onClick(View v) {
        // TODO Auto-generated method stub
        if (statusError == true)
        {
          showDialog(Error_Net);
        }
        else
        {
          startMapsActivity(1);
        }
      }
    });
    
    Button btn_menu_002 = (Button) findViewById(R.id.menu_button2);
    btn_menu_002.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v) {
        // TODO Auto-generated method stub
          startMapsActivity(2);
      }
    });

    Button btn_menu_003 = (Button) findViewById(R.id.menu_button3);
    btn_menu_003.setOnClickListener(new View.OnClickListener() {

      @SuppressWarnings("deprecation")
      public void onClick(View v) {
        // TODO Auto-generated method stub
        if (statusError == true)
        {
          showDialog(Error_Net);
        }
        else
        {
          startMapsActivity(3);
        }
      }
    });
    
    Button btn_menu_004 = (Button) findViewById(R.id.menu_button4);
    btn_menu_004.setOnClickListener(new View.OnClickListener() {

      @SuppressWarnings("deprecation")
      public void onClick(View v) {
        // TODO Auto-generated method stub
        if (statusError == true)
        {
          showDialog(Error_Net);
        }
        else
        {
          startMapsActivity(4);
        }
      }
    });
    
 // APRI TRACCIATI
    Button btn_menu_005 = (Button) findViewById(R.id.manu_button5);
    btn_menu_005.setOnClickListener(new View.OnClickListener() {

      @SuppressWarnings("deprecation")
      public void onClick(View v) {
        // TODO Auto-generated method stub
        if (statusError == true)
        {
          showDialog(Error_Net);
        }
        else
        {
          startMapsActivity(5);
        }
      }
    });

    Button btn_menu_n = (Button) findViewById(R.id.menu_button_n);
    btn_menu_n.setOnClickListener(new View.OnClickListener() {

      @SuppressWarnings("deprecation")
      public void onClick(View v) {
        // TODO Auto-generated method stub
        removeDialog(Error_Net);
        finish();
      }
    });
    //-------------------------------
  }

  @SuppressWarnings("deprecation")
  @Override
  protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    statusError = false;

    if (bridge == null)
    {
      bridge = new BridgeLocation(locationManager, netProvider);
      SingletonParametersBridge.getInstance().addParameter("bridge", bridge);

    }

    // CONTROLLO PROVIDER NETWORK
    if (netProvider == null || !(locationManager.isProviderEnabled(defaultLocationManager))) {
      statusError = true;
    }
    Log_app = "MenuActivity_Status_Error_Provider:"+statusError;
    Log.i(MainActivity.TAG+getLocalClassName(), Log_app);
    //-------------------------------
    //if (statusError == false)
    {
      // CONTROLLO CONNESSIONE INTERNET

      ConnectivityManager connection_manager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

      NetworkInfo activeNetwork = connection_manager.getActiveNetworkInfo();
      boolean isConnected = activeNetwork.isConnectedOrConnecting();
      if (isConnected == false)
      {
        statusError = true;
      }
      Log_app = "MenuActivity_Status_Error_Connection:"+!(isConnected);
      Log.i(MainActivity.TAG+getLocalClassName(), Log_app);
    }
    //-------------------------------
    if (statusError)
    {
      showDialog(Error_Net);
    }
  }

  @Override
  protected void onPause() {
    super.onPause();
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.removeUpdates(myLocationListener);
  }

  private LocationListener myLocationListener = new LocationListener() {
    public void onStatusChanged(String provider, int status, Bundle extras) {
      if (status == LocationProvider.AVAILABLE) {
        statusError = false;
      } else {
        statusError = true;
      }
    }
    public void onProviderEnabled(String provider) {
      statusError = false;
    }
    public void onProviderDisabled(String provider) {
      statusError = true;
    }
    public void onLocationChanged(Location location) {
      // TODO Auto-generated method stub

    }

  };

  protected void startMapsActivity(int i) {
    // TODO Auto-generated method stub
    Intent intent = null;
    switch (i)
    {
    case 1:
      intent = new Intent(this, MapsActivity.class);
      startActivity(intent);
      break;
    case 2:
      intent = new Intent(this, TrackerActivity.class);
      startActivity(intent);
      break;
    case 3:
      intent = new Intent(this, MapRouteActivitySelection.class);
      startActivity(intent);
      break;
    case 4:
      intent = new Intent(this, OpenMapRouteActivity.class);
      startActivity(intent);
      break;
    case 5:
      intent = new Intent(this, OpenTrackRouteActivity.class);
      startActivity(intent);
      break;
    default:
      break;
    }
  }

  @Override
  @Deprecated
  protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    Dialog dialog;
    switch (id) {
    case Error_Net:
      dialog = createError_Net();
      break;
    default:
      dialog = null;
      break;
    }
    return dialog;
  }

  private Dialog createError_Net() {
    // TODO Auto-generated method stub
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Errore: \n Impossibile connettersi alla rete Provider.").setCancelable(false)
    .setPositiveButton("Chiudi", new DialogInterface.OnClickListener() {
      @SuppressWarnings("deprecation")
      public void onClick(DialogInterface dialog, int id) {
        removeDialog(Error_Net);
        //startMapsActivity();
      }
    });
    AlertDialog alert = builder.create();
    return alert;
  }
}
